package ac.svn.accesscontrol.ui.renderer;

import java.awt.Color;
import java.awt.Component;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JTree;
import javax.swing.border.MatteBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;

import ac.svn.accesscontrol.store.SVNGroup;
import ac.svn.accesscontrol.store.SVNUser;
import ac.svn.accesscontrol.utils.Utility;

public class UserTreeCellRenderer extends DefaultListCellRenderer implements TreeCellRenderer 
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public UserTreeCellRenderer()
	{
		super();
		setOpaque(true);
		setBorder(new MatteBorder(0,2,0,0,Color.CYAN));
	}

	public Component getTreeCellRendererComponent(JTree tree, Object value,boolean sel,boolean expanded,boolean leaf,
			int row,boolean hasFocus) 
	{
		//super.getTreeCellRendererComponent(tree, value, sel,  expanded, leaf, row, hasFocus);
		Object nodeObj = ((DefaultMutableTreeNode)value).getUserObject();
		// check whatever you need to on the node user object
		if(nodeObj instanceof SVNGroup)
		{
			SVNGroup svnGroup = (SVNGroup) nodeObj;
			setText(String.format("<HTML><b>%s</b> [%d]",svnGroup.getGroupName(),svnGroup.getGroupMembers().size()));
			setToolTipText(svnGroup.getTooltipText()); 
			setIcon(Utility.getImageIcon("images/groupSmall.png"));
		}
		else if(nodeObj instanceof SVNUser)
		{
			SVNUser svnUser = (SVNUser) nodeObj;
			setText(String.format("<HTML><b>%s</b> [%s]",svnUser.getUserName(),svnUser.getUserAlias()));
			setToolTipText(svnUser.getTooltipText());
			setIcon(Utility.getImageIcon("images/userSmall.png"));
		}
		else
		{
			setText("Accounts");
			setToolTipText("-----------");
			setIcon(Utility.getImageIcon("images/Subversion.png"));
		}

		if(sel)
		{
			setBackground(Color.LIGHT_GRAY);   
		}
		else
		{
			setBackground(Color.WHITE);   
		}


		return this;
	}

}
